<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class product_code extends CI_Controller{
	
	public function search(){
		if($this->check_session()){
			$this->load->Model("product_code_DAO");
			$keyword = $_POST['keyword'];
			$data['product_code_list'] = $this->product_code_DAO->search($keyword);
			$this->parser->parse('table/productcode', $data);
		} else {
			$this->logout();
		}
    }
	
	public function get_list(){
		if($this->check_session()){
			$category_level_2_id = $_POST['category_level_2_id'];
			$category_level_3_id = $_POST['category_level_3_id'];
			$product_fit_id = null;
			if(isset($_POST['product_fit_id'])){
				$product_fit_id = $_POST['product_fit_id'] == 0? null : $_POST['product_fit_id'];
			}
			$product_crotch_id = null;
			if(isset($_POST['product_crotch_id'])){
				$product_crotch_id = $_POST['product_crotch_id'] == 0? null : $_POST['product_crotch_id'];
			}
			$this->load->Model("product_code_DAO");
			$product_code = $this->product_code_DAO->getCode($category_level_2_id, $category_level_3_id, $product_fit_id, $product_crotch_id);
			if(isset($product_code)){
				echo $product_code['code'];
			} else {
				echo '';
			}
		} else {
			$this->logout();
		}
	}
		
	public function get_info(){
		if($this->check_session()){
			$this->load->Model("product_code_DAO");
			$id = $_POST['id'];
			$returnData = $this->product_code_DAO->get($id);
			if($returnData == null){
				echo $returnData;
			} else {
				echo json_encode($returnData);
			}
		} else {
			$this->logout();
		}
	}
	
	public function add(){
		if($this->check_session()){
			$this->load->Model("product_code_DAO");
			$this->load->Model("model/product_code_model");
			$category = new product_code_model();
			$category->category_level_2_id = $_POST['category_level_2_id'];
			$category->category_level_3_id = $_POST['category_level_3_id'];
			if(isset($_POST['product_fit_id'])){
				$category->product_fit_id = $_POST['product_fit_id'] == 0? null : $_POST['product_fit_id'];
			} else {
				$category->product_fit_id = null;
			}
			if(isset($_POST['product_crotch_id'])){
				$category->product_crotch_id = $_POST['product_crotch_id'] == 0? null : $_POST['product_crotch_id'];
			} else {
				$category->product_crotch_id = null;
			}
			$category->code = $_POST['code'];
			$category->description = $_POST['description'];
			echo $this->product_code_DAO->add($category);
		} else {
			$this->logout();
		}
	}
	
	public function update(){
		if($this->check_session()){
			$this->load->Model("product_code_DAO");
			$product_fit_id = null;
			if(isset($_POST['product_fit_id'])){
				$product_fit_id = $_POST['product_fit_id'] == 0? null : $_POST['product_fit_id'];
			}
			$product_crotch_id = null;
			if(isset($_POST['product_crotch_id'])){
				$product_crotch_id = $_POST['product_crotch_id'] == 0? null : $_POST['product_crotch_id'];
			}
			echo $this->product_code_DAO->update($_POST['id'], $_POST['category_level_2_id'], $_POST['category_level_3_id'], $product_fit_id, $product_crotch_id, $_POST['code'], $_POST['description']);
		} else {
			$this->logout();
		}
	}
	
	public function delete(){
		if($this->check_session()){
			$this->load->Model("product_code_DAO");
			echo $this->product_code_DAO->delete($_POST['id']);
		} else {
			$this->logout();
		}
	}
	
	public function logout(){
		$this->session->unset_userdata('session_username');
		$this->session->unset_userdata('session_role');
		redirect('/', 'refresh');
    }
	
	public function check_session(){
		$session_username = $this->session->userdata('session_username');
		return isset($session_username) && $session_username != '';
	}
}